//
//  SettingViewController.m
//  Fantasia
//
//  Created by kongkong on 2016/12/4.
//  Copyright © 2016年 Kongkong. All rights reserved.
//

#import "SettingViewController.h"
#import "LoginViewController.h"
#import "FanUser.h"
#import "AddUserViewController.h"
#import "AddDeviceViewController.h"

typedef enum eSettingControl{
    SettingType_Quit = 0x10000,
    SettingType_AddUser,
    SettingType_AddDevice,
    SettingType_Version,
}SettingControl;

@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView*    tableView;
@property (nonatomic,strong) NSMutableArray*  data;
@property (nonatomic,strong) NSMutableArray*  action;
@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.tableView];
    
    self.data = [@[@"添加设备",[@"版本号" stringByAppendingFormat:@":\t%@.%@",[SettingViewController appVersion],[SettingViewController buildVersion]],@"退出"] mutableCopy];
    self.action = [@[@(SettingType_AddDevice),@(SettingType_Version),@(SettingType_Quit)]mutableCopy];
    
    if ([[FanUser instance].uid isEqualToString:@"admin"]){
        [self.data insertObject:@"添加用户" atIndex:0];
        [self.action insertObject:@(SettingType_AddUser) atIndex:0];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
        _tableView.height -= 44;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

#pragma -mark table

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.data.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = self.data[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch ([self.action[indexPath.row]integerValue]) {
        case SettingType_Quit:
        {
            [[UIViewController rootNavigationController]popToRootViewControllerAnimated:NO];
            
            LoginViewController *login = [[LoginViewController alloc] init];
            [[UIViewController rootNavigationController] pushViewController:login animated:NO];
        }
            break;
        case SettingType_AddUser:{
            [[UIViewController rootNavigationController] pushViewController:[[AddUserViewController alloc]init] animated:YES];
        }
            break;
        case SettingType_AddDevice:{
            [[UIViewController rootNavigationController] pushViewController:[[AddDeviceViewController alloc]init] animated:YES];
            
        }
            break;
        case SettingType_Version:
            break;
        default:
            break;
    }
}

+ (NSString *)appVersion
{
    return [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleShortVersionString"];
}

+ (NSString *)buildVersion
{
    return [[NSBundle mainBundle] objectForInfoDictionaryKey: (NSString *)kCFBundleVersionKey];
}

@end
